package app.common.utils.cjt.ao;

import app.common.utils.cjt.vo.OtherOutboundOrderQueryVo;
import com.gitee.feizns.dynamic.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 其他出库单创建
 * @author feizns
 * @since 2023/3/31
 */
@NoArgsConstructor
@Data
public class OtherDispatchCreateAo {

    /**
     * dto
     */
    private Dto dto;

    /**
     * Dto
     */
    @NoArgsConstructor
    @Data
    public static class Dto {
        /**
         * externalCode
         */
        private String externalCode;

        /**
         * 代码
         */
        private String code;

        /**
         * 是否是修改代码
         */
        private Boolean isModifiedCode = Boolean.TRUE;

        /**
         * voucherType
         */
        private VoucherType voucherType;
        /**
         * voucherDate
         */
        private String voucherDate;
        /**
         * busiType
         */
        private BusiType busiType;
        /**
         * rdStyle
         */
        private RdStyle rdStyle;
        /**
         * warehouse
         */
        private Warehouse warehouse;
        /**
         * department
         */
        private Department department;
        /**
         * clerk
         */
        private Clerk clerk;
        /**
         * project
         */
        private Project project;
        /**
         * dynamicPropertyKeys
         */
        private List<String> dynamicPropertyKeys;
        /**
         * dynamicPropertyValues
         */
        private List<String> dynamicPropertyValues;
        /**
         * memo
         */
        private String memo;
        /**
         * rDRecordDetails
         */
        private RDRecordDetails rDRecordDetailsObj;

        /**
         * rDRecordDetails
         */
        private List<RDRecordDetails> rDRecordDetails = Collections.emptyList();

        /**
         * 从
         * @param order 订单
         * @return {@link Dto}
         */
        public static Dto from(OtherOutboundOrderQueryVo.Order order) {
            Dto dto = new Dto();
            dto.setExternalCode(order.getOrder_no());
            dto.setCode(order.getOrder_no());
            dto.setMemo("编号：[" + order.getOrder_no() + "] 备注:[" + order.getRemark() + "] 类型：[其他出库单]");
            dto.setRdStyle(new RdStyle("208"));
            dto.setVoucherType(new VoucherType("ST1024"));
            dto.setVoucherDate(DateUtils.format(new Date(order.getConsign_time()), "yyyy-MM-dd"));
            dto.setBusiType(new BusiType("13"));
            dto.setWarehouse(new Warehouse(order.getWarehouse_no()));
            List<RDRecordDetails> list = order.getDetail_list().stream().map(RDRecordDetails::from).collect(Collectors.toList());
            dto.setRDRecordDetails(list);
            return dto;
        }

        public List<RDRecordDetails> getRDRecordDetails() {
            return CollectionUtils.isEmpty(rDRecordDetails) ? Collections.singletonList(rDRecordDetailsObj) : rDRecordDetails;
        }

        /**
         * VoucherType
         */
        @NoArgsConstructor
        @Data
        @AllArgsConstructor
        public static class VoucherType {
            /**
             * code
             */
            private String code;
        }

        /**
         * BusiType
         */
        @NoArgsConstructor
        @Data
        public static class BusiType {
            /**
             * code
             */
            private String code;

            public BusiType(String code) {
                this.code = code;
            }
        }

        /**
         * RdStyle
         */
        @NoArgsConstructor
        @Data
        @AllArgsConstructor
        public static class RdStyle {
            /**
             * code
             */
            private String code;
        }

        /**
         * Warehouse
         */
        @NoArgsConstructor
        @Data
        public static class Warehouse {
            /**
             * code
             */
            private String code;

            public Warehouse(String code) {
                this.code = code;
            }
        }

        /**
         * Department
         */
        @NoArgsConstructor
        @Data
        public static class Department {
            /**
             * code
             */
            private String code;
        }

        /**
         * Clerk
         */
        @NoArgsConstructor
        @Data
        public static class Clerk {
            /**
             * code
             */
            private String code;
        }

        /**
         * Project
         */
        @NoArgsConstructor
        @Data
        public static class Project {
            /**
             * code
             */
            private String code;
        }

        /**
         * RDRecordDetails
         */
        @NoArgsConstructor
        @Data
        public static class RDRecordDetails {
            /**
             * invBarCode
             */
            private String invBarCode;
            /**
             * inventory
             */
            private Inventory inventory;
            /**
             * baseQuantity
             */
            private BigDecimal baseQuantity;
            /**
             * project
             */
            private Project project;
            /**
             * productionDate
             */
            private String productionDate;
            /**
             * expiryDate
             */
            private String expiryDate;
            /**
             * invLocation
             */
            private InvLocation invLocation;
            /**
             * batch
             */
            private String batch;
            /**
             * dynamicPropertyKeys
             */
            private List<String> dynamicPropertyKeys;
            /**
             * dynamicPropertyValues
             */
            private List<String> dynamicPropertyValues;
            /**
             * boxNumber
             */
            private String boxNumber;

            /**
             * 量
             */
            private BigDecimal amount;

            /**
             * 价格
             */
            private BigDecimal price;

            public static RDRecordDetails from(OtherOutboundOrderQueryVo.Order.DetailList detailList) {
                RDRecordDetails details = new RDRecordDetails();
                details.setInventory(new Inventory(detailList.getSpec_no()));
                details.setInvLocation(new InvLocation(detailList.getPosition_no()));
                details.setBaseQuantity(BigDecimal.valueOf(detailList.getGoods_count()));
                details.setAmount(BigDecimal.valueOf(detailList.getTotal_amount()));
                if ( detailList.getGoods_count() != 0 )
                    details.setPrice(BigDecimal.valueOf(detailList.getTotal_amount() / detailList.getGoods_count()));
                return details;
            }

            /**
             * Inventory
             */
            @NoArgsConstructor
            @Data
            public static class Inventory {
                /**
                 * code
                 */
                private String code;
                public Inventory(String code) {
                    this.code = code;
                }
            }

            /**
             * Project
             */
            @NoArgsConstructor
            @Data
            public static class Project {
                /**
                 * code
                 */
                private String code;
            }

            /**
             * InvLocation
             */
            @NoArgsConstructor
            @Data
            @AllArgsConstructor
            public static class InvLocation {
                /**
                 * code
                 */
                private String code;
            }
        }
    }
}
